/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import wargame.entity.*;
import wargame.dao.*;

/**
 *
 * @author Clarence
 */
public class ManageBid {

    public static String addABid(int player_id, int project_id, int bid_amount) {

        Project p = ProjectDAO.retrieveProject(project_id);

        if (p.getListing_turn_remaining() > 0) {
            if (bid_amount > 0 && bid_amount <= p.getMax_bid()) {
                // Amount is in acceptable range, can accept

                Bid b = new Bid();
                b.setAmount(bid_amount);
                b.setPlayer_id(player_id);
                b.setProject_id(project_id);

                if (BidDAO.insertBid(b) > 0) {
                    // Game Log
                    GameLog gl = new GameLog();
                    gl.setPlayer_id(player_id);
                    gl.setTurn(GameTurn.current_turn);
                    gl.setDescription("Place a bid on project " + project_id + " for $" + bid_amount);
                    gl.setTimestamp(new Timestamp(new Date().getTime()));
                    GameLogDAO.insertGameLog(gl);
                    return "success";
                }
            } else {
                return "bid amount is invalid or beyond max bid";
            }
        }
        return null;
    }

    public static String editBid(int player_id, int project_id, int bid_amount) {

        Project p = ProjectDAO.retrieveProject(project_id);

        if (p.getListing_turn_remaining() > 0) {
            if (bid_amount > 0 && bid_amount <= p.getMax_bid()) {
                // Amount is in acceptable range, can accept

                Bid b = new Bid();
                b.setAmount(bid_amount);
                b.setPlayer_id(player_id);
                b.setProject_id(project_id);

                if (BidDAO.updateBid(b)) {
                    // Game Log
                    GameLog gl = new GameLog();
                    gl.setPlayer_id(player_id);
                    gl.setTurn(GameTurn.current_turn);
                    gl.setDescription("Update bid on project " + project_id + " to $" + bid_amount);
                    gl.setTimestamp(new Timestamp(new Date().getTime()));
                    GameLogDAO.insertGameLog(gl);
                    return "success";
                }
            } else {
                return "bid amount is invalid or beyond max bid";
            }
        }
        return null;
    }

    public static boolean removeBid(int player_id, int project_id) {
        boolean no_error = false;
        Project p = ProjectDAO.retrieveProject(project_id);

        if (p.getListing_turn_remaining() > 0) {

            // Amount is in acceptable range, can accept

            Bid b = new Bid();
            b.setPlayer_id(player_id);
            b.setProject_id(project_id);

            if (BidDAO.deleteBid(b)) {
                // Game Log
                GameLog gl = new GameLog();
                gl.setPlayer_id(player_id);
                gl.setTurn(GameTurn.current_turn);
                gl.setDescription("Removed bid from project " + project_id);
                gl.setTimestamp(new Timestamp(new Date().getTime()));
                no_error = true;
                GameLogDAO.insertGameLog(gl);

            }
        }
        return no_error;
    }

    // System calls this method upon turn change,
    // This method should be called before decreasing listing_turn_remaining on project
    public static void awardProjects() {
        ArrayList<Project> projects = ProjectDAO.retrieveAllBidableProjects();

        for (int i = 0; i < projects.size(); i++) {

            Project p = projects.get(i);
            ArrayList<Bid> bids = BidDAO.retrieveAllBidsOfProject(p.getProject_id());
            BidComparator comparator = new BidComparator();
            String message = "";

            if (bids.size() > 0) {
                if (bids.size() > 1) {
                     // can put more checks at the BidComparator later.
                    Collections.sort(bids, comparator);
                }

                boolean assign = false;
                for (Bid bid : bids) {
                    Player player = PlayerDAO.retrievePlayer(bid.getPlayer_id());

                    
                    Analytic.createBidAttemptAnalytic(player.getPlayer_id(), bid.getAmount(), p.getMax_bid());
                    if (!assign) {
                        
                        Analytic.createSuccessBidAnalytic(player.getPlayer_id(), bid.getAmount(), p.getMax_bid());
                        p.setTaken_by(player.getPlayer_id());
                        p.setBidded_price(bid.getAmount());
                        p.setListing_turn_remaining(0); // This prevents it from being display again.
                        ProjectDAO.updateProject(p);
                        
                        message = "You have successfully bidded for project #" + p.getProject_id() + " for " + bid.getAmount_string();
                        sendNotification(message, bid.getPlayer_id());
                        assign = true;
                    } else {
                        message = "You have failed to bid for project #" + p.getProject_id() + " for " + bid.getAmount_string();
                        sendNotification(message, bid.getPlayer_id());
                    }
                }
            }

            // Reduce the listing turn remaining
            if (p.getListing_turn_remaining() > 0) {
                p.setListing_turn_remaining(p.getListing_turn_remaining() - 1);
            }

            ProjectDAO.updateProject(p);
        }
    }

    private static void sendNotification(String message, int player_id) {
        // Notify player of bid results
        Notification n = new Notification();
        n.setPlayer_id(player_id);
        n.setMessage("You have " + message);
        n.setTurn(GameTurn.current_turn);
        NotificationDAO.insertNotification(n);

        //Game Log
        GameLog gl = new GameLog();
        gl.setPlayer_id(player_id);
        gl.setTurn(GameTurn.current_turn);
        gl.setDescription(message);
        gl.setTimestamp(new Timestamp(new Date().getTime()));
        GameLogDAO.insertGameLog(gl);

    }
}
